import type { App, Plugin } from "vue"; // 只是导入类型不是导入App的值

/**
 * 组件外部使用use时执行install，然后将组件注册为全局
 */

// 类型必须导出否则生成不了.d.ts文件
export type SFCWithInstall<T> = T & Plugin;

export const withInstall = <T>(component: T, alias?: string) => {
  const comp = component as SFCWithInstall<T>;
  comp.install = (app: App) => {
    app.component((comp as any).name, component as SFCWithInstall<T>);
    if (alias) {
      app.config.globalProperties[alias] = component;
    }
  };
  return comp as SFCWithInstall<T>;
};

// ==== isNumber  函数====
const toString = Object.prototype.toString;
export function is(val: any, type: any) {
  return toString.call(val) === `[object ${type}]`;
}
export function isNumber(val: any) {
  return is(val, "Number");
}

export function isFunction(variable: any): variable is Function {
  return typeof variable === "function";
}

export function formatTime(totalSeconds: number) {
  console.log(totalSeconds);
  const hours = Math.floor(totalSeconds / 3600);
  const minutes = Math.floor((totalSeconds % 3600) / 60);
  const seconds = Math.floor(totalSeconds % 60);

  console.log(hours, minutes, seconds);

  if (minutes == 0 && hours == 0) {
    return seconds;
  } else if (hours == 0) {
    return `${minutes}:${seconds}`;
  } else {
    return `${hours}:${minutes}:${seconds}`;
  }
}
